<?php
Namespace Cyfy\Modules\Forms;

use Cyfy\Message;

/**
 * Module with form API.
 *
 * See classes in namespace \Cyfy\Modules\Forms for more information.
 */
class Module extends \Cyfy\Module
{
    /**
     * Char, which represents a required item.
     */
    const REQUIRED_CHAR = "<span class=\"form-required-star\">*</span>";

    /**
     * Whether a form has been on.
     */
    static private $hasRun = false;
    
    /**
     * Showing of '*You must fill these inputs.' under forms.
     */
    static public $requiredCaption = false;

    /**
     * Sending the form via AJAX 
     */
    static function ajax($name)
    {
        switch($name)
        {
            case "SendForm":
                return self :: ajaxSendForm();
        }
    }
    
    /**
     * Ajax has been sent.
     */
    static function ajaxSendForm()
    {
        // checking...
        if($_POST["formModule"] && $_POST["formName"]){
            $class = "\Cyfy\Modules\\" . $_POST["formModule"] . "\Forms\\" . $_POST["formName"];
            
            // does the form exist?
            if(class_exists($class)){
            
                // creates an instance
                $form = new $class;
                
                // loads data form post
                $form -> loadPostData();
                
                // validation
                if($form -> autoValidate())
                {
                    if($form -> validate())
                    {
                        // finishing
                        $form -> autoSubmit();
                        
                        // reload data - an autosubmit method can change something
                        $form -> loadPostData();
                        
                        // custom submit
                        $success = $form -> submit();
                        
                        //returns data
                        return array("success"=> $success, "onSubmit" => $form -> onSubmit, "location" => $form -> action, "stay" => $form -> stay);
                    }
                }
             
            }else{
                Message :: set(self :: t(150), Message::ERROR);
            }
        }else{
            Message :: set(self :: t(160), Message::ERROR);
        }        
        return array("success"=>false, "location" => "");
    }
    
    /**
     * Checks the forms and after sends them.
     * THIS IS USED FOR NON-AJAX SENDING, WHERE JAVASCRIPT IS NOT AVAILABLE
     */
    static function checkForms(){
        // checking...
        if($_POST["formModule"] && $_POST["formName"])
        {
            $class = "\Cyfy\Modules\\" . $_POST["formModule"] . "\Forms\\" . $_POST["formName"];
            
            // does the form exist?
            if(class_exists($class)){
            
                // creates an instance
                $form = new $class;
                
                // loads data form post
                $form -> loadPostData();
                
                // validation
                if($form -> autoValidate())
                {
                    if($form -> validate())
                    {
                        // finishing
                        $form -> autoSubmit();
                        
                        // reload data
                        $form -> loadPostData();
                        
                        // custom submit
                        $success = $form -> submit();
                        
                        if($form -> action != ""){
                            // location
                            $location = \Cyfy\Cyfy :: getPath($form -> action);
                            // redirecting
                            @header("Location: ".$location);
                            // if the redirecting doesn't work - print a link
                            print("<a href=\"{$location}\">". self :: t(170)."</a>");
                            include_once("_end.php");
                        }
                    }
                }
            }else{
                Message :: set(self :: t(150), Message :: ERROR);
            }
        }
    }
    
    /**
     * Includes scripts into header.
     */
    static function runonce()
    {
        if(!self::$hasRun)
        {
            \Cyfy\Cyfy ::  javaScript(\Cyfy\Cyfy ::  getSourcePath("Cyfy/Modules/Forms/forms.js"), "file");
            self :: $hasRun = true;
        }
    }
}




    